#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
struct Node {
  int val, pri;
  int sz;
  int ch[2];
} tr[N];
#define val(x) tr[x].val
#define pri(x) tr[x].pri
#define sz(x) tr[x].sz
#define lc(x) tr[x].ch[0]
#define rc(x) tr[x].ch[1]
int root;
int newnode(int v) {
  static int tot = 0;
  ++tot;
  val(tot) = v;
  pri(tot) = rand();
  sz(tot) = 1;
  lc(tot) = rc(tot) = 0;
  return tot;
}
void pushup(int p) { sz(p) = sz(lc(p)) + sz(rc(p)) + 1; }
void split(int p, int v, int &l, int &r) {
  if (!p) {
    l = r = 0;
    return;
  }
  if (val(p) <= v) {
    l = p;
    split(rc(l), v, rc(l), r);
    pushup(l);
  } else {
    r = p;
    split(lc(r), v, l, lc(r));
    pushup(r);
  }
}
int merge(int x, int y) {
  if (!x || !y) return x + y;
  if (pri(x) >= pri(y)) {
    rc(x) = merge(rc(x), y);
    pushup(x);
    return x;
  } else {
    lc(y) = merge(x, lc(y));
    pushup(y);
    return y;
  }
}
void insert(int v) {
  int x, y, z;
  split(root, v, x, z);
  y = newnode(v);
  root = merge(x, merge(y, z));
}
void del(int v) {
  int x, y, z;
  split(root, v, x, z);
  split(x, v - 1, x, y);
  y = merge(lc(y), rc(y));
  root = merge(x, merge(y, z));
}
int get_rk(int v) {
  int x, y, ans;
  split(root, v - 1, x, y);
  ans = sz(x);
  root = merge(x, y);
  return ans;
}
int get_val(int p, int k) {
  if (k <= sz(lc(p)))
    return get_val(lc(p), k);
  else if (k <= sz(lc(p)) + 1)
    return val(p);
  else
    return get_val(rc(p), k - sz(lc(p)) - 1);
}
int get_pre(int v) {
  int x, y;
  split(root, v - 1, x, y);
  int ans = get_val(x, sz(x));
  root = merge(x, y);
  return ans;
}
int get_suc(int v) {
  int x, y;
  split(root, v, x, y);
  int ans = get_val(y, 1);
  root = merge(x, y);
  return ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  insert(-1e9), insert(1e9);
  int n, op, x;
  cin >> n;
  while (n--) {
    cin >> op >> x;
    if (op == 1)
      insert(x);
    else if (op == 2)
      del(x);
    else if (op == 3)
      cout << get_rk(x) << endl;
    else if (op == 4)
      cout << get_val(root, x + 1) << endl;
    else if (op == 5)
      cout << get_pre(x) << endl;
    else
      cout << get_suc(x) << endl;
  }
  return 0;
}